/*
 *	Perlin Production Co. Ltd. September 1993 (c).
 *			GRAB3.C
 */

#include "stdio.h"
#include "wios.h"
#include "conio.h"
#include "graphics.h"
#include "grab.h"

#define	BYE		(& bwin)

putbar(xs,ys,xe,ye)
int xs,ys,xe,ye;
{
	register int i;

	for (i=xs,ptscnt=points ; i < xe ; i++) store(i,ys);
	for ( i=ys ; i < ye ; i++ )		store(xe,i);
	for ( i=xe ; i > xs ; i-- )		store(i,ye);
	for ( i=ye ; i > ys ; i-- )		store(xs,i);
}


putimg(xs,ys,xe,ye)
int xs,ys,xe,ye;
{
	register int i;

	for (i=xs,ptscnt=points ; i < xe ; i++) restore(i,ys);
	for ( i=ys ; i < ye ; i++ )		restore(xe,i);
	for ( i=xe ; i > xs ; i-- )		restore(i,ye);
	for ( i=ye ; i > ys ; i-- )		restore(xs,i);
}


restore(x,y)
int x,y;
{

	if ((*ptscnt++) == 0) _SETCOL=0;
	else _SETCOL=6;
	putpixel(x,y);
}


store(x,y)
int x,y;
{
	if ( (*ptscnt++=getpixel(x,y)) == 0 ) _SETCOL=6;
	else _SETCOL=0;
	putpixel(x,y);
}


trouble(s1,s2)
char *s1,*s2;
{
	WINDOW wtrouble;
	int xs2pos;

	defs_wn(1,18,76,5,H_BORD,TROUBLE);
	sw_color(REV_VID,TROUBLE);
	sb_color(REV_VID,TROUBLE);
	sw_name("Errors during execution.",TROUBLE);
	set_wn(TROUBLE);
	mv_csr(0,1,TROUBLE);
	rev_vid();
	w_puts(s1,TROUBLE);
	w_putchar(BLANK,TROUBLE);
	rev_vid();
	if (errno) perror(s2);
	else w_puts(s2,TROUBLE);

	/* First time I used isfd() , but it works very funny */

	if ( dst != ERROR )
	{
		if ((close(dst)) == ERROR)
		{
			mv_csr(10,2,TROUBLE);
			w_puts("Error closing destination file.",TROUBLE);
			puts("\007");
		}
	}
	nrm_vid();
	goto_xy(0,MAXY);
	sh_csr();
	exit(0);
}


bye_bye()
{
	WINDOW	bwin,wtrouble;

	def_wn(0,0,MAXX,4,S_BORD,BYE);
	sw_shadow(NO_SHADOW,BYE);
	cls();
	set_wn(BYE);
	mv_csr(20,1,BYE);
	w_puts("Thank you for using GRAB!",BYE);
	if ( dst != ERROR )
	{
		if ((close(dst)) == ERROR)
		{
			defs_wn(1,18,76,5,H_BORD,TROUBLE);
			sw_color(REV_VID,TROUBLE);
			sb_color(REV_VID,TROUBLE);
			sw_name("You've got some problems",TROUBLE);
			set_wn(TROUBLE);
			mv_csr(10,1,TROUBLE);
			w_puts("Error closing destination file.",TROUBLE);
			puts("\007");
		}
		else unlink(dstname);
	}
	goto_xy(0,MAXY);
	sh_csr();
	exit(0);
}
